package com.ruoyi.hxf.service.impl;


import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hxf.controller.TopicController;
import com.ruoyi.hxf.domain.Course;
import com.ruoyi.hxf.domain.TeacherCourse;
import com.ruoyi.hxf.domain.Type;
import com.ruoyi.hxf.mapper.RosterMapper;
import com.ruoyi.hxf.mapper.SubjectMapper;
import com.ruoyi.hxf.mapper.TypeMapper;
import com.ruoyi.hxf.service.IRosterService;
import com.ruoyi.hxf.service.ITypeService;
import com.ruoyi.system.domain.App;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 课程的管理Service业务层处理
 *
 * @author hxf
 * @date 2022-12-16
 */
@RequiredArgsConstructor
@Service
public class RosterServiceImpl implements IRosterService {
    private final ITypeService iTypeService;
    private final RosterMapper rosterMapper;
    private final TypeMapper typeMapper;
    private final ISysUserService userService;


    /**
     * 课表的建立和修改
     *
     * @param courses 全部从早到下午的课程
     * @return
     */
    @Override
    public Boolean insertByBo(List<Course> courses) {
        StringBuffer stringBuffer = new StringBuffer();
        Long classId = courses.get(1).getClassId();
        String old = typeMapper.selectTeacherIdCount(classId);
        typeMapper.deleteClassTeacher(classId);
        for (Course course : courses) {
            Boolean Existence = rosterMapper.ExistenceClassCourse(course.getId(), course.getClassId());
            if (Existence) {
                Course course1 = rosterMapper.selectClassInformation(course.getId(), course.getClassId());
                Long iDelOne = rosterMapper.selectTeacherId(course1.getOneT(), course1.getOneS());
                if (iDelOne != null) {
                    TeacherCourse teacherCourseDelOne = new TeacherCourse();
                    teacherCourseDelOne.setId(course1.getId());
                    teacherCourseDelOne.setLabel(course1.getLabel());
                    teacherCourseDelOne.setTeacherId(Long.valueOf(iDelOne));
                    teacherCourseDelOne.setOneC(null);
                    rosterMapper.updateTeacherCourseOne(teacherCourseDelOne.getOneC(), teacherCourseDelOne.getId(), teacherCourseDelOne.getLabel(), teacherCourseDelOne.getTeacherId());
                }
                Long iReNewOne = rosterMapper.selectTeacherId(course.getOneT(), course.getOneS());
                if (iReNewOne != null) {
                    TeacherCourse teacherCourseOne = new TeacherCourse();
                    teacherCourseOne.setId(course.getId());
                    teacherCourseOne.setLabel(course.getLabel());
                    teacherCourseOne.setTeacherId(Long.valueOf(iReNewOne));
                    teacherCourseOne.setOneC(rosterMapper.selectClassName(course.getClassId()));
                    Boolean IfThatOne = rosterMapper.selectTeacherCourse(teacherCourseOne.getId(), teacherCourseOne.getLabel(), teacherCourseOne.getTeacherId());
                    if (IfThatOne) {
                        rosterMapper.updateTeacherCourseOne(teacherCourseOne.getOneC(), teacherCourseOne.getId(), teacherCourseOne.getLabel(), teacherCourseOne.getTeacherId());
                    } else {
                        rosterMapper.insertTeacherCourse(teacherCourseOne);
                    }
                }

                Long iDelTwo = rosterMapper.selectTeacherId(course1.getTwoT(), course1.getTwoS());
                if (iDelTwo != null) {
                    TeacherCourse teacherCourseDelTwo = new TeacherCourse();
                    teacherCourseDelTwo.setId(course1.getId());
                    teacherCourseDelTwo.setLabel(course1.getLabel());
                    teacherCourseDelTwo.setTeacherId(Long.valueOf(iDelTwo));
                    teacherCourseDelTwo.setTwoC(null);
                    rosterMapper.updateTeacherCourseTwo(teacherCourseDelTwo.getTwoC(), teacherCourseDelTwo.getId(), teacherCourseDelTwo.getLabel(), teacherCourseDelTwo.getTeacherId());
                }
                Long iReNewTwo = rosterMapper.selectTeacherId(course.getTwoT(), course.getTwoS());
                if (iReNewTwo != null) {
                    TeacherCourse teacherCourseTwo = new TeacherCourse();
                    teacherCourseTwo.setId(course.getId());
                    teacherCourseTwo.setLabel(course.getLabel());
                    teacherCourseTwo.setTeacherId(Long.valueOf(iReNewTwo));
                    teacherCourseTwo.setTwoC(rosterMapper.selectClassName(course.getClassId()));
                    Boolean IfThatTwo = rosterMapper.selectTeacherCourse(teacherCourseTwo.getId(), teacherCourseTwo.getLabel(), teacherCourseTwo.getTeacherId());
                    if (IfThatTwo) {
                        rosterMapper.updateTeacherCourseTwo(teacherCourseTwo.getTwoC(), teacherCourseTwo.getId(), teacherCourseTwo.getLabel(), teacherCourseTwo.getTeacherId());
                    } else {
                        rosterMapper.insertTeacherCourse(teacherCourseTwo);
                    }
                }
                Long iDelThree = rosterMapper.selectTeacherId(course1.getThreeT(), course1.getThreeS());
                if (iDelThree != null) {
                    TeacherCourse teacherCourseDelThree = new TeacherCourse();
                    teacherCourseDelThree.setId(course1.getId());
                    teacherCourseDelThree.setLabel(course1.getLabel());
                    teacherCourseDelThree.setTeacherId(Long.valueOf(iDelThree));
                    teacherCourseDelThree.setThreeC(null);
                    rosterMapper.updateTeacherCourseThree(teacherCourseDelThree.getThreeC(), teacherCourseDelThree.getId(), teacherCourseDelThree.getLabel(), teacherCourseDelThree.getTeacherId());
                }
                Long iReNewThree = rosterMapper.selectTeacherId(course.getThreeT(), course.getThreeS());
                if (iReNewThree != null) {
                    TeacherCourse teacherCourseThree = new TeacherCourse();
                    teacherCourseThree.setId(course.getId());
                    teacherCourseThree.setLabel(course.getLabel());
                    teacherCourseThree.setTeacherId(Long.valueOf(iReNewThree));
                    teacherCourseThree.setThreeC(rosterMapper.selectClassName(course.getClassId()));
                    Boolean IfThatThree = rosterMapper.selectTeacherCourse(teacherCourseThree.getId(), teacherCourseThree.getLabel(), teacherCourseThree.getTeacherId());
                    if (IfThatThree) {
                        rosterMapper.updateTeacherCourseThree(teacherCourseThree.getThreeC(), teacherCourseThree.getId(), teacherCourseThree.getLabel(), teacherCourseThree.getTeacherId());
                    } else {
                        rosterMapper.insertTeacherCourse(teacherCourseThree);
                    }
                }
                Long iDelFour = rosterMapper.selectTeacherId(course1.getFourT(), course1.getFourS());
                if (iDelFour != null) {
                    TeacherCourse teacherCourseDelFour = new TeacherCourse();
                    teacherCourseDelFour.setId(course1.getId());
                    teacherCourseDelFour.setLabel(course1.getLabel());
                    teacherCourseDelFour.setTeacherId(Long.valueOf(iDelFour));
                    teacherCourseDelFour.setFourC(null);
                    rosterMapper.updateTeacherCourseFour(teacherCourseDelFour.getFourC(), teacherCourseDelFour.getId(), teacherCourseDelFour.getLabel(), teacherCourseDelFour.getTeacherId());
                }
                Long iReNewFour = rosterMapper.selectTeacherId(course.getFourT(), course.getFourS());
                if (iReNewFour != null) {
                    TeacherCourse teacherCourseFour = new TeacherCourse();
                    teacherCourseFour.setId(course.getId());
                    teacherCourseFour.setLabel(course.getLabel());
                    teacherCourseFour.setTeacherId(Long.valueOf(iReNewFour));
                    teacherCourseFour.setFourC(rosterMapper.selectClassName(course.getClassId()));
                    Boolean IfThatFour = rosterMapper.selectTeacherCourse(teacherCourseFour.getId(), teacherCourseFour.getLabel(), teacherCourseFour.getTeacherId());
                    if (IfThatFour) {
                        rosterMapper.updateTeacherCourseFour(teacherCourseFour.getFourC(), teacherCourseFour.getId(), teacherCourseFour.getLabel(), teacherCourseFour.getTeacherId());
                    } else {
                        rosterMapper.insertTeacherCourse(teacherCourseFour);
                    }
                }
                Long iDelFive = rosterMapper.selectTeacherId(course1.getFiveT(), course1.getFiveS());
                if (iDelFive != null) {
                    TeacherCourse teacherCourseDelFive = new TeacherCourse();
                    teacherCourseDelFive.setId(course1.getId());
                    teacherCourseDelFive.setLabel(course1.getLabel());
                    teacherCourseDelFive.setTeacherId(Long.valueOf(iDelFive));
                    teacherCourseDelFive.setFiveC(null);
                    rosterMapper.updateTeacherCourseFive(teacherCourseDelFive.getFiveC(), teacherCourseDelFive.getId(), teacherCourseDelFive.getLabel(), teacherCourseDelFive.getTeacherId());
                }
                Long iReNewFive = rosterMapper.selectTeacherId(course.getFiveT(), course.getFiveS());
                if (iReNewFive != null) {
                    TeacherCourse teacherCourseFive = new TeacherCourse();
                    teacherCourseFive.setId(course.getId());
                    teacherCourseFive.setLabel(course.getLabel());
                    teacherCourseFive.setTeacherId(Long.valueOf(iReNewFive));
                    teacherCourseFive.setFiveC(rosterMapper.selectClassName(course.getClassId()));
                    Boolean IfThatFive = rosterMapper.selectTeacherCourse(teacherCourseFive.getId(), teacherCourseFive.getLabel(), teacherCourseFive.getTeacherId());
                    if (IfThatFive) {
                        rosterMapper.updateTeacherCourseFive(teacherCourseFive.getFiveC(), teacherCourseFive.getId(), teacherCourseFive.getLabel(), teacherCourseFive.getTeacherId());
                    } else {
                        rosterMapper.insertTeacherCourse(teacherCourseFive);
                    }
                }
                Long iDelSix = rosterMapper.selectTeacherId(course1.getSixT(), course1.getSixS());
                if (iDelSix != null) {
                    TeacherCourse teacherCourseDelSix = new TeacherCourse();
                    teacherCourseDelSix.setId(course1.getId());
                    teacherCourseDelSix.setLabel(course1.getLabel());
                    teacherCourseDelSix.setTeacherId(Long.valueOf(iDelSix));
                    teacherCourseDelSix.setSixC(null);
                    rosterMapper.updateTeacherCourseSix(teacherCourseDelSix.getSixC(), teacherCourseDelSix.getId(), teacherCourseDelSix.getLabel(), teacherCourseDelSix.getTeacherId());
                }
                Long iReNewSix = rosterMapper.selectTeacherId(course.getSixT(), course.getSixS());
                if (iReNewSix != null) {
                    TeacherCourse teacherCourseSix = new TeacherCourse();
                    teacherCourseSix.setId(course.getId());
                    teacherCourseSix.setLabel(course.getLabel());
                    teacherCourseSix.setTeacherId(Long.valueOf(iReNewSix));
                    teacherCourseSix.setSixC(rosterMapper.selectClassName(course.getClassId()));
                    Boolean IfThatSix = rosterMapper.selectTeacherCourse(teacherCourseSix.getId(), teacherCourseSix.getLabel(), teacherCourseSix.getTeacherId());
                    if (IfThatSix) {
                        rosterMapper.updateTeacherCourseSix(teacherCourseSix.getSixC(), teacherCourseSix.getId(), teacherCourseSix.getLabel(), teacherCourseSix.getTeacherId());
                    } else {
                        rosterMapper.insertTeacherCourse(teacherCourseSix);
                    }
                }
                course.setUpdateBy(String.valueOf(LoginHelper.getUserId()));
                course.setUpdateTime(DateUtils.getNowDate());
                rosterMapper.update(course);

            } else {
                course.setCreateBy(String.valueOf(LoginHelper.getUserId()));
                course.setCreateTime(DateUtils.getNowDate());
                rosterMapper.insert(course);
                Long iOne = rosterMapper.selectTeacherId(course.getOneT(), course.getOneS());
                if (iOne != null) {
                    TeacherCourse teacherCourseOne = new TeacherCourse();
                    teacherCourseOne.setId(course.getId());
                    teacherCourseOne.setLabel(course.getLabel());
                    teacherCourseOne.setTeacherId(Long.valueOf(iOne));
                    teacherCourseOne.setOneC(rosterMapper.selectClassName(course.getClassId()));
                    Boolean IfThatOne = rosterMapper.selectTeacherCourse(teacherCourseOne.getId(), teacherCourseOne.getLabel(), teacherCourseOne.getTeacherId());
                    if (IfThatOne) {
                        rosterMapper.updateTeacherCourseOne(teacherCourseOne.getOneC(), teacherCourseOne.getId(), teacherCourseOne.getLabel(), teacherCourseOne.getTeacherId());
                    } else {
                        rosterMapper.insertTeacherCourse(teacherCourseOne);
                    }
                }
                Long iTwo = rosterMapper.selectTeacherId(course.getTwoT(), course.getTwoS());
                if (iTwo != null) {
                    TeacherCourse teacherCourseTwo = new TeacherCourse();
                    teacherCourseTwo.setId(course.getId());
                    teacherCourseTwo.setLabel(course.getLabel());
                    teacherCourseTwo.setTeacherId(Long.valueOf(iTwo));
                    teacherCourseTwo.setTwoC(rosterMapper.selectClassName(course.getClassId()));
                    Boolean IfThatTwo = rosterMapper.selectTeacherCourse(teacherCourseTwo.getId(), teacherCourseTwo.getLabel(), teacherCourseTwo.getTeacherId());
                    if (IfThatTwo) {
                        rosterMapper.updateTeacherCourseTwo(teacherCourseTwo.getTwoC(), teacherCourseTwo.getId(), teacherCourseTwo.getLabel(), teacherCourseTwo.getTeacherId());
                    } else {
                        rosterMapper.insertTeacherCourse(teacherCourseTwo);
                    }
                }
                Long iThree = rosterMapper.selectTeacherId(course.getThreeT(), course.getThreeS());
                if (iThree != null) {
                    TeacherCourse teacherCourseThree = new TeacherCourse();
                    teacherCourseThree.setId(course.getId());
                    teacherCourseThree.setLabel(course.getLabel());
                    teacherCourseThree.setTeacherId(Long.valueOf(iThree));
                    teacherCourseThree.setThreeC(rosterMapper.selectClassName(course.getClassId()));
                    Boolean IfThatThree = rosterMapper.selectTeacherCourse(teacherCourseThree.getId(), teacherCourseThree.getLabel(), teacherCourseThree.getTeacherId());
                    if (IfThatThree) {
                        rosterMapper.updateTeacherCourseThree(teacherCourseThree.getThreeC(), teacherCourseThree.getId(), teacherCourseThree.getLabel(), teacherCourseThree.getTeacherId());
                    } else {
                        rosterMapper.insertTeacherCourse(teacherCourseThree);
                    }
                }
                Long iFour = rosterMapper.selectTeacherId(course.getFourT(), course.getFourS());
                if (iFour != null) {
                    TeacherCourse teacherCourseFour = new TeacherCourse();
                    teacherCourseFour.setId(course.getId());
                    teacherCourseFour.setLabel(course.getLabel());
                    teacherCourseFour.setTeacherId(Long.valueOf(iFour));
                    teacherCourseFour.setFourC(rosterMapper.selectClassName(course.getClassId()));
                    Boolean IfThatFour = rosterMapper.selectTeacherCourse(teacherCourseFour.getId(), teacherCourseFour.getLabel(), teacherCourseFour.getTeacherId());
                    if (IfThatFour) {
                        rosterMapper.updateTeacherCourseFour(teacherCourseFour.getFourC(), teacherCourseFour.getId(), teacherCourseFour.getLabel(), teacherCourseFour.getTeacherId());
                    } else {
                        rosterMapper.insertTeacherCourse(teacherCourseFour);
                    }
                }
                Long iFive = rosterMapper.selectTeacherId(course.getFiveT(), course.getFiveS());
                if (iFive != null) {
                    TeacherCourse teacherCourseFive = new TeacherCourse();
                    teacherCourseFive.setId(course.getId());
                    teacherCourseFive.setLabel(course.getLabel());
                    teacherCourseFive.setTeacherId(Long.valueOf(iFive));
                    teacherCourseFive.setFiveC(rosterMapper.selectClassName(course.getClassId()));
                    Boolean IfThatFive = rosterMapper.selectTeacherCourse(teacherCourseFive.getId(), teacherCourseFive.getLabel(), teacherCourseFive.getTeacherId());
                    if (IfThatFive) {
                        rosterMapper.updateTeacherCourseFive(teacherCourseFive.getFiveC(), teacherCourseFive.getId(), teacherCourseFive.getLabel(), teacherCourseFive.getTeacherId());
                    } else {
                        rosterMapper.insertTeacherCourse(teacherCourseFive);
                    }
                }
                Long iSix = rosterMapper.selectTeacherId(course.getSixT(), course.getSixS());
                if (iSix != null) {
                    TeacherCourse teacherCourseSix = new TeacherCourse();
                    teacherCourseSix.setId(course.getId());
                    teacherCourseSix.setLabel(course.getLabel());
                    teacherCourseSix.setTeacherId(Long.valueOf(iSix));
                    teacherCourseSix.setSixC(rosterMapper.selectClassName(course.getClassId()));
                    Boolean IfThatSix = rosterMapper.selectTeacherCourse(teacherCourseSix.getId(), teacherCourseSix.getLabel(), teacherCourseSix.getTeacherId());
                    if (IfThatSix) {
                        rosterMapper.updateTeacherCourseSix(teacherCourseSix.getSixC(), teacherCourseSix.getId(), teacherCourseSix.getLabel(), teacherCourseSix.getTeacherId());
                    } else {
                        rosterMapper.insertTeacherCourse(teacherCourseSix);
                    }
                }
            }
            Long jOne = rosterMapper.selectTeacherId(course.getOneT(), course.getOneS());
            Long jTwo = rosterMapper.selectTeacherId(course.getTwoT(), course.getTwoS());
            Long jThree = rosterMapper.selectTeacherId(course.getThreeT(), course.getThreeS());
            Long jFour = rosterMapper.selectTeacherId(course.getFourT(), course.getFourS());
            Long jFive = rosterMapper.selectTeacherId(course.getFiveT(), course.getFiveS());
            Long jSix = rosterMapper.selectTeacherId(course.getSixT(), course.getSixS());
            if (stringBuffer.length() == 0 && jOne != null) {
                stringBuffer.append(jOne);
            } else if (stringBuffer.length() != 0 && jOne != null) {
                int x = 0;
                String[] split = stringBuffer.toString().split(",");
                for (String s : split) {
                    if (s.equals(String.valueOf(jOne))) {
                        x = 1;
                        break;
                    }
                }
                if (x == 0) {
                    stringBuffer.append("," + jOne);
                }

            }
            if (stringBuffer.length() == 0 && jTwo != null) {
                stringBuffer.append(jTwo);
            } else if (stringBuffer.length() != 0 && jTwo != null) {
                int x = 0;
                String[] split = stringBuffer.toString().split(",");
                for (String s : split) {
                    if (s.equals(String.valueOf(jTwo))) {
                        x = 1;
                        break;
                    }
                }
                if (x == 0) {
                    stringBuffer.append("," + jTwo);
                }
            }
            if (stringBuffer.length() == 0 && jThree != null) {
                stringBuffer.append(jThree);
            } else if (stringBuffer.length() != 0 && jThree != null) {
                int x = 0;
                String[] split = stringBuffer.toString().split(",");
                for (String s : split) {
                    if (s.equals(String.valueOf(jThree))) {
                        x = 1;
                        break;
                    }
                }
                if (x == 0) {
                    stringBuffer.append("," + jThree);
                }
            }
            if (stringBuffer.length() == 0 && jFour != null) {
                stringBuffer.append(jFour);
            } else if (stringBuffer.length() != 0 && jFour != null) {
                int x = 0;
                String[] split = stringBuffer.toString().split(",");
                for (String s : split) {
                    if (s.equals(String.valueOf(jFour))) {
                        x = 1;
                        break;
                    }
                }
                if (x == 0) {
                    stringBuffer.append("," + jFour);
                }
            }
            if (stringBuffer.length() == 0 && jFive != null) {
                stringBuffer.append(jFive);
            } else if (stringBuffer.length() != 0 && jFive != null) {
                int x = 0;
                String[] split = stringBuffer.toString().split(",");
                for (String s : split) {
                    if (s.equals(String.valueOf(jFive))) {
                        x = 1;
                        break;
                    }
                }
                if (x == 0) {
                    stringBuffer.append("," + jFive);
                }
            }
            if (stringBuffer.length() == 0 && jSix != null) {
                stringBuffer.append(jSix);
            } else if (stringBuffer.length() != 0 && jSix != null) {
                int x = 0;
                String[] split = stringBuffer.toString().split(",");
                for (String s : split) {
                    if (s.equals(String.valueOf(jSix))) {
                        x = 1;
                        break;
                    }
                }
                if (x == 0) {
                    stringBuffer.append("," + jSix);
                }
            }
        }
        if (old!=null && !old.equals("")) {
            String[] split1 = old.split(",");
            String[] split2 = stringBuffer.toString().split(",");
            int temp = 0;
            for (int i = 0; i < split1.length; i++) {
                for (int j = 0; j < split2.length; j++) {
                    if (split1[i].equals(split2[j])) {
                        temp = 1;
                        break;
                    }
                }
                if (temp == 0) {
                    App app = userService.selectAppUserByClassId(Long.valueOf(split1[i]));
                    if (app != null) {
                        String classIds = app.getClassId();
                        StringBuffer appList = new StringBuffer();
                        if (classIds.length() != 0) {
                            String[] split = classIds.split(",");
                            for (int k = 0; k < split.length; k++) {
                                if (Integer.parseInt(split[k]) != classId) {
                                    appList.append(split[k] + ",");
                                }
                            }
                            userService.updateUserByApp(appList.substring(0, appList.length() - 1), Long.valueOf(split1[i]));
                        }
                    }
                } else {
                    temp = 0;
                }
            }
        }
        if (stringBuffer.length() > 0
            && !"null".equals(stringBuffer.toString())
            && !"".equals(stringBuffer.toString())) {
            String[] split1 = stringBuffer.toString().split(",");
            for (int i = 0; i < split1.length; i++) {
                App app = userService.selectAppUserByClassId(Long.valueOf(split1[i]));
                if (app != null) {
                    String classIds = app.getClassId();
                    StringBuffer appList = new StringBuffer();
                    if (classIds.length() != 0) {
                        String[] split = classIds.split(",");
                        for (int k = 0; k < split.length; k++) {
                            if (Integer.parseInt(split[k]) != classId) {
                                appList.append(split[k] + ",");
                            }
                        }
                        appList.append(classId + ",");


                        userService.updateUserByApp(appList.substring(0, appList.length() - 1), Long.valueOf(split1[i]));
                    }
                }
            }
        }
        typeMapper.insertClassTeacher(stringBuffer.toString(), classId);
        String s = iTypeService.selectRoleName();
        Long OldClassLeader = iTypeService.selectTypeByDeptId(classId);
        if (OldClassLeader != null) {
            Boolean integer = typeMapper.selectExistMasterId(classId, OldClassLeader);
            if (integer == false) {
                iTypeService.updateUserRole(OldClassLeader, s);
                Type typeClearLeader = new Type();
                typeClearLeader.setDeptId(classId);
                typeClearLeader.setClassLeader(null);
                iTypeService.updateByType(typeClearLeader);
            }
        }
        return true;
    }

    /**
     * 根据班级ID查询班级的课程表
     *
     * @param classId 班级ID
     * @return
     */
    @Override
    public List<Course> listClassCourse(String classId) {
        return rosterMapper.listClassCourse(classId);
    }

    /**
     * 根据班级的ID逻辑删除
     *
     * @param classId 班级ID
     * @return
     */
    @Override
    public int deleteWithValidByIds(String classId) {
        String old = typeMapper.selectTeacherIdCount(Long.valueOf(classId));
        if (old!=null && !old.equals("")) {
            String[] split1 = old.split(",");
            for(int k=0;k<split1.length;k++){
                App app = userService.selectAppUserByClassId(Long.valueOf(split1[k]));
                if(app!=null){
                    String classIds = app.getClassId();
                    StringBuffer appList=new StringBuffer();
                    if(classIds.length()!=0){
                        String[] split = classIds.split(",");
                        for (int i = 0; i < split.length; i++) {
                            if(Integer.parseInt(split[i])!=Integer.parseInt(classId)){
                                appList.append(split[i]+",");
                            }
                        }
                        userService.updateUserByApp(appList.substring(0,appList.length()-1), Long.valueOf(split1[k]));
                    }
                }
            }
        }

        typeMapper.deleteClassTeacher(Long.valueOf(classId));
        String s = iTypeService.selectRoleName();
        Long OldClassLeader = iTypeService.selectTypeByDeptId(Long.valueOf(classId));
        if (OldClassLeader != null) {
            iTypeService.updateUserRole(OldClassLeader, s);
            Type typeClearLeader = new Type();
            typeClearLeader.setDeptId(Long.valueOf(classId));
            typeClearLeader.setClassLeader(null);
            iTypeService.updateByType(typeClearLeader);
        }
        List<Course> courses = rosterMapper.listClassCourse(classId);
        for (Course course : courses) {
            Long iDelOne = rosterMapper.selectTeacherId(course.getOneT(), course.getOneS());
            if (iDelOne != null) {
                TeacherCourse teacherCourseDelOne = new TeacherCourse();
                teacherCourseDelOne.setId(course.getId());
                teacherCourseDelOne.setLabel(course.getLabel());
                teacherCourseDelOne.setTeacherId(Long.valueOf(iDelOne));
                teacherCourseDelOne.setOneC(null);
                rosterMapper.updateTeacherCourseOne(teacherCourseDelOne.getOneC(), teacherCourseDelOne.getId(), teacherCourseDelOne.getLabel(), teacherCourseDelOne.getTeacherId());
            }
            Long iDelTwo = rosterMapper.selectTeacherId(course.getTwoT(), course.getTwoS());
            if (iDelTwo != null) {
                TeacherCourse teacherCourseDelTwo = new TeacherCourse();
                teacherCourseDelTwo.setId(course.getId());
                teacherCourseDelTwo.setLabel(course.getLabel());
                teacherCourseDelTwo.setTeacherId(Long.valueOf(iDelTwo));
                teacherCourseDelTwo.setTwoC(null);
                rosterMapper.updateTeacherCourseTwo(teacherCourseDelTwo.getTwoC(), teacherCourseDelTwo.getId(), teacherCourseDelTwo.getLabel(), teacherCourseDelTwo.getTeacherId());
            }
            Long iDelThree = rosterMapper.selectTeacherId(course.getThreeT(), course.getThreeS());
            if (iDelThree != null) {
                TeacherCourse teacherCourseDelThree = new TeacherCourse();
                teacherCourseDelThree.setId(course.getId());
                teacherCourseDelThree.setLabel(course.getLabel());
                teacherCourseDelThree.setTeacherId(Long.valueOf(iDelThree));
                teacherCourseDelThree.setThreeC(null);
                rosterMapper.updateTeacherCourseThree(teacherCourseDelThree.getThreeC(), teacherCourseDelThree.getId(), teacherCourseDelThree.getLabel(), teacherCourseDelThree.getTeacherId());
            }
            Long iDelFour = rosterMapper.selectTeacherId(course.getFourT(), course.getFourS());
            if (iDelFour != null) {
                TeacherCourse teacherCourseDelFour = new TeacherCourse();
                teacherCourseDelFour.setId(course.getId());
                teacherCourseDelFour.setLabel(course.getLabel());
                teacherCourseDelFour.setTeacherId(Long.valueOf(iDelFour));
                teacherCourseDelFour.setFourC(null);
                rosterMapper.updateTeacherCourseFour(teacherCourseDelFour.getFourC(), teacherCourseDelFour.getId(), teacherCourseDelFour.getLabel(), teacherCourseDelFour.getTeacherId());
            }
            Long iDelFive = rosterMapper.selectTeacherId(course.getFiveT(), course.getFiveS());
            if (iDelFive != null) {
                TeacherCourse teacherCourseDelFive = new TeacherCourse();
                teacherCourseDelFive.setId(course.getId());
                teacherCourseDelFive.setLabel(course.getLabel());
                teacherCourseDelFive.setTeacherId(Long.valueOf(iDelFive));
                teacherCourseDelFive.setFiveC(null);
                rosterMapper.updateTeacherCourseFive(teacherCourseDelFive.getFiveC(), teacherCourseDelFive.getId(), teacherCourseDelFive.getLabel(), teacherCourseDelFive.getTeacherId());
            }
            Long iDelSix = rosterMapper.selectTeacherId(course.getSixT(), course.getSixS());
            if (iDelSix != null) {
                TeacherCourse teacherCourseDelSix = new TeacherCourse();
                teacherCourseDelSix.setId(course.getId());
                teacherCourseDelSix.setLabel(course.getLabel());
                teacherCourseDelSix.setTeacherId(Long.valueOf(iDelSix));
                teacherCourseDelSix.setSixC(null);
                rosterMapper.updateTeacherCourseSix(teacherCourseDelSix.getSixC(), teacherCourseDelSix.getId(), teacherCourseDelSix.getLabel(), teacherCourseDelSix.getTeacherId());
            }
        }
        return rosterMapper.deleteWithValidByIds(classId);
    }

}
